/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test_system.parser;

import test_system.questions.Answer;
import test_system.questions.CheckAnswerQuestion;
import test_system.questions.Question;
import test_system.questions.QuestionWrapper;
import test_system.questions.TestWrapper;
import test_system.questions.TextAnswerQuestion;

/**
 *
 * @author root
 */
public class ParseRefactor {

    private QuestionWrapper createQuestion(String questionString) {
        Question question;
        String[] questionData = questionString.split(",");
        if (questionData.length == 2) {
            Answer rightAnswer = new Answer(questionData[1], true);
            return new QuestionWrapper(new TextAnswerQuestion(questionData[0], rightAnswer));
        }
        Answer[] answerVars = createAnswerVars(questionData);
        return new QuestionWrapper(new CheckAnswerQuestion(questionData[0], answerVars));
    }
    

    private Answer[] createAnswerVars(String[] questionData) {
        Answer rightAnswer = new Answer(questionData[1], true);
        Answer[] answerVars = new Answer[questionData.length - 1];
        answerVars[0] = rightAnswer;
        for (int i = 2; i < questionData.length; i++) {
            answerVars[i - 1] = new Answer(questionData[i], false);
        }
        return answerVars;

    }
    
    public TestWrapper createTest(StringBuffer parseResult){
        String[] testData = parseResult.toString().split(";");
        QuestionWrapper[] test = new QuestionWrapper[testData.length];
        for(int i = 0; i < testData.length; i++){
            test[i] = createQuestion(testData[i]);
        }
        return new TestWrapper(test);
    }
    

}
